var sugar = {
    userReport: null,
    loader: null,

    initLoader: function(){
        if(this.loader == null){
            this.loader = $('#loader').html();
        }
        return this.loader;
    },

    runToggleTable: function(){
        $('table.mainRow tbody').css('display', 'none');

        $('table.mainRow th.iconPlusMinus') .click(function(ev){
            var userId = ev.currentTarget.id;
            sugar.userReport = $('#'+ev.currentTarget.id).parent().parent().parent().find('tbody td');
            if(sugar.userReport.html() == ""){
                sugar.userReport.html(sugar.initLoader());
                sugar.fetchUserReport(userId);
            }


            var img, mainRowTab = $(this).parents('table.mainRow');

            mainRowTab.children('tbody').toggle();
            img = mainRowTab.children('thead').find('th.iconPlusMinus img');
            if(img.attr('src').indexOf('plus') != -1){
                img.attr('src', 'themes/default/images/minus.gif');
            }else{
                img.attr('src', 'themes/default/images/plus.gif');
            }
        });
    },
    
    fetchUserReport: function(userId){
        $.ajax({
                url: "index.php?module=Dashboard&action=loadUserReport&to_pdf=true",
                type: 'post',
                data: {
                    from: $('#from').val(),
                    to: $('#to').val(),
                    "userId": userId
                },
                success: function( data ) {
                    sugar.userReport.html(data);
                    sugar.userReport.addClass('userReportTd');
                    $('.userReportRows').hover(
                        function(){
                            $(this).css('background-color', 'white');
                        },
                        function(){
                            $(this).css('background-color', '#F7FAB6');
                        }
                    );
                }
            });
    }
};